﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.ManageFrontEnd
{
    public partial class ManageMenuHeader : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            using (DALDataContext d = new DALDataContext())
            {
                grdMenuHeader.DataSource = d.MenuHeaders.OrderBy(i=>i.OrderDisplay);
                grdMenuHeader.DataBind();
            }
        }
        protected void grdAccount_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using (DALDataContext d = new DALDataContext())
                    {
                        MenuHeader menuHeader = new MenuHeader();
                        TextBox txtName = (TextBox) e.Item.FindControl("txtName");
                        TextBox txtUrl = (TextBox) e.Item.FindControl("txtUrl");
                        TextBox txtOrderDisplay = (TextBox) e.Item.FindControl("txtOrderDisplay");
                        menuHeader.Name = txtName.Text.Trim();
                        menuHeader.Url = txtUrl.Text.Trim();
                        menuHeader.IsActive = true;
                        if (A.M.IsValidNumber(txtOrderDisplay.Text))
                            menuHeader.OrderDisplay = Convert.ToInt32(txtOrderDisplay.Text);
                        else
                            menuHeader.OrderDisplay = 100;
                        d.MenuHeaders.InsertOnSubmit(menuHeader);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdUpdate":
                    using (DALDataContext d = new DALDataContext())
                    {
                        MenuHeader menuHeader = d.MenuHeaders.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        TextBox txtName = (TextBox)e.Item.FindControl("txtName");
                        TextBox txtUrl = (TextBox)e.Item.FindControl("txtUrl");
                        TextBox txtOrderDisplay = (TextBox)e.Item.FindControl("txtOrderDisplay");
                        menuHeader.Name = txtName.Text.Trim();
                        menuHeader.Url = txtUrl.Text.Trim();
                        if (A.M.IsValidNumber(txtOrderDisplay.Text))
                            menuHeader.OrderDisplay = Convert.ToInt32(txtOrderDisplay.Text);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdDelete":
                    using (DALDataContext d = new DALDataContext())
                    {
                        MenuHeader menuHeader = d.MenuHeaders.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        d.MenuHeaders.DeleteOnSubmit(menuHeader);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdIsActive":
                    using (DALDataContext d = new DALDataContext())
                    {
                        MenuHeader menuHeader = d.MenuHeaders.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        menuHeader.IsActive = !menuHeader.IsActive;
                        d.SubmitChanges();
                    }
                    break;
            }
            BindData();
        }
    }
}